﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

namespace PSDLib.Ctrl
{
    public partial class DiskInfo : UserControl
    {
        public delegate void DiskDriverChangedEventHandler(DiskInfo disk, string driverName);
        public delegate void SetStorageTypeEventHanlder(int storageType, int group);
        public event DiskDriverChangedEventHandler DiskDriverChanged;
        public event EventHandler MainStorageChanged;
        public event EventHandler SecondrayStorageChanged;
        public event EventHandler SetNormalStorage;
        public event EventHandler DiskClick;

        public DiskInfo()
        {
            InitializeComponent();



            updateCtrlText = new updateCtrlTextEevntHandler(delegate (Control ctrl, string text)
            {
                ctrl.Text = text;
            });

            updateProgressBar = new updateProgressBarEventHandler(delegate (ProgressBar pb, int value)
            {
                if (value >= pb.Minimum && value <= pb.Maximum)
                {
                    pb.Value = value;
                }
            });

            updateStorageType = new SetStorageTypeEventHanlder(delegate (int storageType, int group)
            {

                //switch (Convert.ToInt32(sender))
                //{
                //    case 0:
                //        picStorageType.Image = Properties.Resources.Drive_Generic_0;
                //        break;
                //    case 1:
                //        picStorageType.Image = Properties.Resources.Drive_Generic_1;
                //        break;
                //    case 2:
                //        picStorageType.Image = Properties.Resources.Drive_Generic_2;
                //        break;
                //    default:
                //        picStorageType.Image = Properties.Resources.Drive_Generic_0;
                //        break;
                //}

                if (storageType == 0 && group == 1)
                {
                    picStorageType.Image = Properties.Resources.storage_main_1;
                }
                else if (storageType == 0 && group == 2)
                {
                    picStorageType.Image = Properties.Resources.storage_main_2;
                }
                else if (storageType == 1 && group == 1)
                {
                    picStorageType.Image = Properties.Resources.storage_backup_1;
                }
                else if (storageType == 1 && group == 2)
                {
                    picStorageType.Image = Properties.Resources.storage_backup_2;
                }
                else
                {
                    picStorageType.Image = Properties.Resources.storage_not_set;
                }

            });

            updateDownloading = new EventHandler(delegate (object sender, EventArgs e)
            {
                this.lblDownloading.Visible = Convert.ToBoolean(sender);
            });

            Downloading = false;
        }

        private string volumeName = "";
        private string diskDriver = "";
        private double freeSpace = 0;
        private double sumSpace = 0;
        private int storageType = 0;
        private int group = 0;
        private bool downloading = false;

        private delegate void updateCtrlTextEevntHandler(Control ctrl, string text);
        private updateCtrlTextEevntHandler updateCtrlText;
        private delegate void updateProgressBarEventHandler(ProgressBar pb, int value);
        private updateProgressBarEventHandler updateProgressBar;
        private SetStorageTypeEventHanlder updateStorageType;
        private EventHandler updateDownloading;
        /// <summary>
        /// 磁盘分区名称
        /// </summary>
        public string VolumeName
        {
            get { return volumeName; }
            set
            {
                volumeName = value;

                if (this.InvokeRequired)
                {
                    this.Invoke(updateCtrlText, lblDiskLabel, volumeName + "(" + diskDriver + ")");
                }
                else
                {
                    updateCtrlText(lblDiskLabel, volumeName + "(" + diskDriver + ")");
                }
            }
        }

        /// <summary>
        /// 磁盘分区盘符
        /// </summary>
        public string DiskDriver
        {
            get
            {
                return diskDriver;
            }
            set
            {
                diskDriver = value;

                if (this.InvokeRequired)
                {
                    this.Invoke(updateCtrlText, lblDiskLabel, volumeName + "(" + diskDriver + ")");
                }
                else
                {
                    updateCtrlText(lblDiskLabel, volumeName + "(" + diskDriver + ")");
                }

                DiskDriverChanged?.Invoke(this, diskDriver);
            }
        }

        /// <summary>
        /// 磁盘分区可用容量
        /// </summary>
        public double FreeSpace
        {
            get
            {
                return freeSpace;
            }
            set
            {
                freeSpace = value;

                if (this.InvokeRequired)
                {
                    this.Invoke(updateCtrlText, lblDiskSpace, PSDFunction.FormatByteSize(freeSpace) + "可用，共" + PSDFunction.FormatByteSize(sumSpace));
                }
                else
                {
                    updateCtrlText(lblDiskSpace, PSDFunction.FormatByteSize(freeSpace) + "可用，共" + PSDFunction.FormatByteSize(sumSpace));
                }

                UpdateProgressBar();
            }
        }

        /// <summary>
        /// 磁盘分区总容量
        /// </summary>
        public double SumSpace
        {
            get
            {
                return sumSpace;
            }
            set
            {
                sumSpace = value;

                if (this.InvokeRequired)
                {
                    this.Invoke(updateCtrlText, lblDiskSpace, PSDFunction.FormatByteSize(freeSpace) + "可用，共" + PSDFunction.FormatByteSize(sumSpace));
                }
                else
                {
                    updateCtrlText(lblDiskSpace, PSDFunction.FormatByteSize(freeSpace) + "可用，共" + PSDFunction.FormatByteSize(sumSpace));
                }

                UpdateProgressBar();
            }
        }

        public int StorageType
        {
            get
            {
                return storageType;
            }
            set
            {
                storageType = value;

                if (this.InvokeRequired)
                {
                    this.Invoke(updateStorageType, storageType, group);
                }
                else
                {
                    updateStorageType(storageType, group);
                }
            }
        }

        public int Group
        {
            get
            {
                return group;
            }
            set
            {
                group = value;

                if (this.InvokeRequired)
                {
                    this.Invoke(updateStorageType, storageType, group);
                }
                else
                {
                    updateStorageType(storageType, group);
                }
            }
        }

        private void UpdateProgressBar()
        {
            int val = (sumSpace > 0 ? Convert.ToInt32((sumSpace - freeSpace) / sumSpace * 100) : 0);

            if (this.InvokeRequired)
            {
                this.Invoke(updateProgressBar, progressBar1, val);
            }
            else
            {
                updateProgressBar(progressBar1, val);
            }
        }

        public bool Downloading
        {
            get
            {
                return downloading;
            }
            set
            {
                downloading = value;

                if (this.InvokeRequired)
                {
                    this.Invoke(updateDownloading, downloading, null);
                }
                else
                {
                    updateDownloading(downloading, null);
                }

            }
        }



        //private string GetSpaceText(double space)
        //{
        //    string text = "";

        //    string[] unit = new string[] { "B", "KB", "MB", "GB", "TB", "PB" };

        //    for (int i = 1; i <= unit.Length; i++)
        //    {
        //        if (space / Math.Pow(1024, i) < 1)
        //        {
        //            text = Math.Round(space / Math.Pow(1024, i - 1), 2).ToString() + unit[i - 1];
        //            break;
        //        }
        //    }

        //    return text;
        //}

        private void miSetMainStorage_Click(object sender, EventArgs e)
        {
            //if (DeviceInfo.Device != null)
            //{
            //    DeviceInfo.SetMainStorage(this);
            //}

            MainStorageChanged?.Invoke(this, e);
        }

        private void miSetSecondaryStorage_Click(object sender, EventArgs e)
        {
            //if (DeviceInfo.Device != null)
            //{
            //    DeviceInfo.SetSecondaryStorage(this);
            //}
            SecondrayStorageChanged?.Invoke(this, e);
        }

        private void miCancel_Click(object sender, EventArgs e)
        {
            //if (DeviceInfo.Device != null)
            //{
            //    DeviceInfo.SetNormalStorage(this);
            //}
            SetNormalStorage?.Invoke(this, e);
        }

        private void Disk_Click(object sender, EventArgs e)
        {
            DiskClick?.Invoke(this, e);
        }
    }

    public enum DownloadStorageType : int
    {
        normal = 0,
        main = 1,
        secondary = 2
    }
}
